﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExtendModule;

namespace LabMS.Web.Equipment.Instrument
{
    public partial class InstrumentPreview : Base.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            strPagePrivilege.Add("SYYQGL");//必须写在ispostback外
            strPageUserType.Add("mana");
            strPageUserType.Add("teacher");
            strPageUserType.Add("student");
            tbCreateDateMin.Attributes.Add("onclick", "c.showMoreDay = false;c.show(this)");
            tbCreateDateMax.Attributes.Add("onclick", "c.showMoreDay = false;c.show(this)");
            if (!Page.IsPostBack)
            {
                bind("");
                lbDelete.Attributes.Add("onclick", "javascript:return confirm('你确认要删除这些易耗品吗?')");
            }
            tbCreateDateMin.Attributes.Add("readOnly", "-1");
            tbCreateDateMax.Attributes.Add("readOnly", "-1");
        }

        protected void lbaddinstrument_Click(object sender, EventArgs e)
        {
            Response.Redirect("addinstrument.aspx");
        }

        public void bind(string strWhere)
        {
            List<ExtendModule.InstrumentAndConsumables> listinstrument = new
                List<InstrumentAndConsumables>();
            ExtendBLL.InstrumentAndConsumables beconsum = new ExtendBLL.InstrumentAndConsumables();
            listinstrument = beconsum.GetExtendModelList(" Type = 'yiqi' " + strWhere + " order by ID desc ");
            LabMS.BLL.Lab blab = new LabMS.BLL.Lab();
            List<LabMS.Model.Lab> mLab = new List<LabMS.Model.Lab>();
            ExtendBLL.Dictionary eDictionary = new ExtendBLL.Dictionary();
            List<Model.Dictionary> listCClass = new List<LabMS.Model.Dictionary>();
            listCClass = eDictionary.GetItemListByDClass("haocaileibie");
            foreach (ExtendModule.InstrumentAndConsumables i in listinstrument)
            {
                mLab = blab.GetModelList(" Lab_Code='" +i.StockLocation+"'");
                if (mLab.Count > 0)
                {
                    i.StockLocation = mLab[0].Lab_Name;
                }
                listCClass = eDictionary.GetModelList(" Code='"+ i.Source+"' ");
                if (listCClass.Count > 0)
                {
                    i.Source = listCClass[0].Name;
                }
                listCClass = eDictionary.GetModelList(" Code='" + i.Arrow + "' ");
                if (listCClass.Count > 0)
                {
                    i.Arrow = listCClass[0].Name;
                }
            }

            gvInstrument.DataSource = listinstrument;
            gvInstrument.DataBind();
        }

        protected void gvInstrument_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvInstrument.PageIndex = e.NewPageIndex;
            if (tbKeyWords.Text.Trim().Equals("") && tbCreateDateMin.Text.Trim().Equals("") &&
                     tbCreateDateMax.Text.Trim().Equals("") && tbPurpose.Text.Trim().Equals(""))
            {
                bind("");
            }
            else
            {
                bind(SearchString());
            }

        }

        protected void gvInstrument_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //e.Row.Attributes.Add("OnMouseOut", "this.style.backgroundColor='White';this.style.color='#003399'");
                //e.Row.Attributes.Add("OnMouseOver", "this.style.backgroundColor='#6699FF';this.style.color='#8C4510'");
                e.Row.Attributes["style"] = "Cursor:pointer";
                int itemp = e.Row.Cells.Count;
                for (int i = 1; i < itemp; i++)
                {
                    e.Row.Cells[i].Attributes.Add("OnDblClick", "window.location.href='instrumentDetailInfo.aspx?ID="+ gvInstrument.DataKeys[gvInstrument.DataKeys.Count - 1].Value.ToString() + "'");
                }


            }
        }

        //搜索查询语句
        protected string SearchString()
        {
            string strWhere = " and " + ddlCClass.SelectedValue + " like '%" + tbKeyWords.Text.Trim() + "%' ";
            if (!string.IsNullOrEmpty(tbCreateDateMin.Text.Trim()))
            {
                strWhere += " and CreatedDate >= '" + tbCreateDateMin.Text.Trim() + "'";
            }
            if (!string.IsNullOrEmpty(tbCreateDateMax.Text.Trim()))
            {
                strWhere += " and CreatedDate <= '" + tbCreateDateMax.Text.Trim() + "'";
            }
            strWhere += " and Purpose like '%" + tbPurpose.Text.Trim() + "%'";

            return strWhere;
        }

        protected void btSearch_Click(object sender, EventArgs e)
        {
            bind(SearchString());
        }

        protected void btUpdate_Click(object sender, EventArgs e)
        {
            tbKeyWords.Text = "";
            tbPurpose.Text = "";
            tbCreateDateMin.Text = "";
            tbCreateDateMax.Text = "";
            bind("");
        }

        protected void btDel_Click(object sender, EventArgs e)
        {
            lbErr.Visible = false;
            CheckBox ch;
            LabMS.BLL.InstrumentAndConsumables bconsum = new LabMS.BLL.InstrumentAndConsumables();
            try
            {
                for (int i = 0; i < gvInstrument.Rows.Count; i++)
                {
                    ch = (CheckBox)gvInstrument.Rows[i].FindControl("checkdel");
                    if (ch.Checked)
                    {
                        string CID = gvInstrument.DataKeys[i].Value.ToString();
                        bconsum.Delete(long.Parse(CID));
                    }
                }
                //checkall.Checked = false;
                if (tbKeyWords.Text.Trim().Equals("") && tbCreateDateMin.Text.Trim().Equals("") &&
                     tbCreateDateMax.Text.Trim().Equals("") && tbPurpose.Text.Trim().Equals(""))
                {
                    bind("");
                }
                else
                {
                    bind(SearchString());
                }

                Common.JShelper.JSAlert(Page, "success", "删除成功!");
            }
            catch (Exception ex)
            {
                Common.JShelper.JSAlert(Page, "err", "删除失败!");
                lbErr.Visible = true;
                lbErr.Text = ex.Message;
            }
        }

        protected void gvInstrument_RowCreated(object sender, GridViewRowEventArgs e)
        {
            if (UserType != "mana")
            {
                if (e.Row.RowType != DataControlRowType.EmptyDataRow && e.Row.RowType != DataControlRowType.Pager)
                {
                    e.Row.Cells[0].Visible = false;
                }
            }
        }
    }
}
